﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using FTBS.Models.Entities;

namespace FTBS.Models.DAO
{
    public class CommentDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// Load comment by Film
        /// </summary>
        /// <returns>list of comment</returns>
        /// <author>hungph</author>
        public List<Comment> LoadComments(int filmId)
        {
            List<Comment> listComments = new List<Comment>();
            try
            {
                var comments = from comment in _entities.Comments
                               where comment.IsDeleted == false && comment.Status == true && comment.FilmId == filmId
                               orderby comment.CommentId descending
                               select comment;
                listComments = comments.ToList();
            }
            catch
            {
                return listComments;
            }
            return listComments;
        }

        /// <summary>
        /// Load comment By Id
        /// </summary>
        /// <returns>Comment</returns>
        /// <author>hungph</author>
        public Comment LoadCommentById(int id)
        {
            Comment comment = new Comment();
            try
            {
                comment = (from c in _entities.Comments
                           where c.CommentId == id
                                && c.IsDeleted == false
                           select c).FirstOrDefault();
                return comment;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Add a Comment
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>hungph</author>
        public int Add(Comment comment)
        {
            try
            {
                _entities.Comments.Add(comment);
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                return 0;
                throw ex;
            }
        }

        /// <summary>
        /// Edit a comment
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>hungph</author>
        public int Edit(Comment comment)
        {
            try
            {
                _entities.Entry(comment).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                return 0;
                throw ex;
            }
        }

        /// <summary>
        /// Delete a Comment
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>hungph</author>
        public int Delete(Comment comment)
        {
            try
            {
                comment.IsDeleted = true;
                _entities.Entry(comment).State = EntityState.Modified;
                int result = _entities.SaveChanges();
                return result;
            }
            catch (Exception ex)
            {
                return 0;
                throw ex;
            }
        }
    }
}